package com.edu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.bean.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;


@WebServlet("/updatePwd.do")
public class UpdatePwdServlet extends HttpServlet {
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object u = request.getSession().getAttribute("loginUser");
		if(u == null){
				response.sendRedirect("login.jsp");
				return;
		}
		User loginUser = (User)u;
		UserService us = new UserServiceImpl();
		request.setCharacterEncoding("utf-8");
		String username = loginUser.getUsername();
		String oldPwd = request.getParameter("oldPwd");
		String newPwd = request.getParameter("newPwd");
		
		int result = us.updateUser(username, oldPwd, newPwd);
		if(result == -1) {
			request.setAttribute("msg", "原始密码错误无法修改");
			request.getRequestDispatcher("updatePwd.jsp").forward(request, response);
		}
		
		if(result == 1) {
			request.getSession().invalidate();
			response.sendRedirect("login.jsp");
		}
	}

}
